﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using ATW.MT.BUS;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;

namespace ATW.MT.CommonClass
{
    /// <summary>
    /// 安特威管理后台页面通用类，所有子页面均继承此类
    /// </summary>
    public class CommonPage : Page
    {
        bus_D_user user = new bus_D_user();
        protected override void OnInit(EventArgs e)
        {
            try
            {
                UserInfo u = null;
                if (Session["UserInfo"] == null)
                {
                    Server.Transfer("../Main/Login.aspx");
                    return;
                }
                u = (UserInfo)Session["UserInfo"];
                DataTable dt = user.getModuleList(u.UserId);
                bool flag = false;
                if (dt != null && dt.Rows.Count > 0)
                {
                    string page = Path.GetFileName(Request.Url.ToString()).ToLower();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string path = dt.Rows[i]["module_url"].ToString();
                        string[] args = path.Split(new char[] { '/' });
                        string _page = args[args.Length - 1].ToLower();
                        if (page == _page)
                        {
                            flag = true;
                            break;
                        }
                    }

                }
                if (!flag)
                {
                    Server.Transfer("../Default/Unauthoried.aspx");
                    return;
                }

            }
            catch (Exception ex)
            {

            }
            base.OnInit(e);

        }

        #region 页面方法

        /// <summary>
        /// 打卡模态窗口
        /// </summary>
        /// <param name="name">窗口名称</param>
        /// <param name="url">窗口url</param>
        /// <param name="args">窗口宽和高，不需要加单位(height=xxx,width=xxx )</param>
        public void OpenDialog(string name, string url, string args)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.AppendFormat(@"<script language=javascript>
                                        window.open('{0}','{1}','z-lock=yes,top=300,left=300,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,channelmode=yes,status=no,{2}')
                                    </script>", url, name, args);
            this.ClientScript.RegisterStartupScript(this.GetType(), "openDialog", strScript.ToString());
        }
        /// <summary>
        /// 打开模态窗口(暂定)(需要Jquery,子窗口需定义window.returnValue）
        /// </summary>
        /// <param name="name">窗口名称</param>
        /// <param name="url">窗口url</param>
        /// <param name="args">窗口参数</param>
        /// <param name="postBack">回发控件名称</param>
        public void OpenDialog(string name, string url, string args, string postBack)
        {
            string strScript = "";
            strScript += "<script type=\"text/javascript\">";
            strScript += "var res=window.showModalDialog('" + url + "','" + name + "','" + args + "');";
            strScript += "if(res!=undefined){";
            strScript += "var btn=$(\"[id$='" + postBack + "']\");";
            strScript += "btn.click();";
            strScript += "}";
            strScript += "</script>";
            this.ClientScript.RegisterStartupScript(this.GetType(), "openDialogWithValue", strScript);
        }
        /// <summary>
        /// 打开窗口
        /// </summary>
        /// <param name="name">窗口名称</param>
        /// <param name="url">窗口url</param>
        /// <param name="args">窗口参数</param>
        public void OpenWindow(string name, string url, string args)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.AppendFormat(@"<script language=javascript>window.open('{0}','{1}','{2}')</script>", url, name, args);
            this.ClientScript.RegisterStartupScript(this.GetType(), "openWin", strScript.ToString());
        }
        /// <summary>
        /// 后台弹出提示框
        /// </summary>
        /// <param name="msg">消息内容</param>
        /// <param name="type">1表示成功，2表示失败，3表示警告</param>
        public void ShowMsgTips(string msg, int type)
        {
            string style = "msgOk";
            if (type == 1)
            {
                style = "position: fixed;left: 50%;font-size:14px;top: 50%;border: 1px none lightgray;padding: 10px 10px 10px 10px; background-color:#D91913;";
            }
            if (type == 2)
            {
                style = "position: fixed;left: 50%;font-size:14px;top: 50%;border: 1px none lightgray;padding: 10px 10px 10px 10px;background-color: #D91913;";
            }
            if (type == 3)
            {
                style = "position: fixed;left: 50%;font-size:14px;top: 50%;border: 1px none lightgray;padding: 10px 10px 10px 10px;background-color: #D91913;";
            }
            string res = "<div id=\"backEndMsgTips\" style=\"" + style + "\" onclick=\"javascript:this.style.display='none'\">";
            res += "<span style=\"font-size:14px;color:white\">";
            res += msg;
            res += "</span>";
            res += "<script type=\"text/javascript\">";
            res += "(function(){  var div=document.getElementById('backEndMsgTips');window.setTimeout(function(){div.style.display='none'},1000) }())";
            res += "</script>";
            res += "</div>";
            Response.Write(res);
        }
        /// <summary>
        /// 关闭窗口，刷新父窗口
        /// </summary>
        public void CloseWindowRefresh()
        {
            string strScript = "<script language=javascript>";
            strScript += "if(window.opener){";
            strScript += "window.opener.location.href=window.opener.location.href";
            strScript += "};";
            strScript += "window.close()";
            strScript += "</script>";
            this.ClientScript.RegisterStartupScript(this.GetType(), "closeWin", strScript.ToString());
        }
        #endregion
    }
}